package com.cool4j.framework.ds.impl;

import com.cool4j.framework.core.ConfigHelper;
import com.cool4j.framework.ds.DataSourceFactory;

import javax.sql.DataSource;

/**
 * @author Frank create on 2016/1/4 14:52
 */
public abstract class AbstractDataSourceFactory<T extends DataSource> implements DataSourceFactory {

  protected final String driver = ConfigHelper.getString("cool.framework.jdbc.driver");
  protected final String url = ConfigHelper.getString("cool.framework.jdbc.url");
  protected final String username = ConfigHelper.getString("cool.framework.jdbc.username");
  protected final String passwrod = ConfigHelper.getString("cool.framework.jdbc.password");

  @Override
  public final T getDataSource() {
    // 创建数据源对象
    T ds = createDataSource();
    // 设置基础属性
    setDriver(ds, driver);
    setUrl(ds, url);
    setUsername(ds, username);
    setPassword(ds, passwrod);
    // 设置高级属性
    setAdvancedConfig(ds);
    return ds;
  }

  public abstract T createDataSource();

  public abstract void setDriver(T ds, String driver);

  public abstract void setUrl(T ds, String url);

  public abstract void setUsername(T ds, String username);

  public abstract void setPassword(T ds, String passwrod);

  public abstract void setAdvancedConfig(T ds);
}
